<template>
	<div class="library_permission">
		<div class="handle">
			<permi-checkbox v-for="(item, index) in libButton" :key="index" :title="item.label" :data="item.children" v-model="item.checked"></permi-checkbox>
		</div>
		<div class="note">以上权限配置适用于所有资源库</div>
	</div>
</template>
<script>
import { mapGetters } from 'vuex';
import permiCheckbox from './permiCheckbox';
import { getLibraryButtonList } from '@/api/platform/tbRoleButton.js';
export default {
	components: { permiCheckbox },
	data() {
		return {
			libButton: []
		};
	},
	computed: { ...mapGetters(['dictEnums']) },
	mounted() {
		this.getLibraryButtonData();
	},
	methods: {
		//获取库按钮数据
		async getLibraryButtonData() {
			let params = { page: 1, limit: 9999 };
			let result = await getLibraryButtonList(params).then((res) => res.data);
			if (result.code === '0') {
				let datas = !result.data ? [] : result.data;
				this.libButton = this.libButtonGrouping(datas);
			}
		},

		//库按钮分组
		libButtonGrouping(arr) {
			let result = arr.reduce((t, v) => {
				return !t[v['type']] && (t[v['type']] = []), t[v['type']].push(v), t;
			}, {});
			return this.dictEnums['DISK_ROLE_BUTTON_TYPE'].map((item) => {
				return {
					label: item.msg,
					checked: result[item.value].filter((item) => item.isSelect === '1').map((item) => item.id),
					children: result[item.value].reverse()
				};
			});
		},

		//保存配置
		handleSaveConfig() {
			let params = [];
			this.libButton.forEach((item) => {
				params = [...params, ...item.checked];
			});
		}
	}
};
</script>
<style lang="scss" scoped>
.library_permission {
	flex: 1;
	overflow: auto;
	padding: 16px;
	.note {
		color: #909399;
	}
}
</style>
